// D:\work\TopDFM_JNS\qt5.6.3-win32-msvc2015\topcam\topjs\3.4.0\bin\topjs3.exe
// 从数据库解密脚本
var fs = require('fs');
var db = require("topsin.database");
var crypto = require('topsin.crypto');

var script_name = "JTtest"  // 脚本名称

db.addConnection({   // 数据库信息
    databse_type:db.DbType.pg,
    database_host:'139.196.104.13:5433',  // 139.196.104.13:5433
    database_name:'TOPDFM_JTG_V6',
    database_user:'toplinker',
    database_pwd:'TopLinker0510'
});

// 从数据库获取脚本
var ret = db.query("", function (d) {
    return d.selectMap({
        table:'pdm_workprocess_script',
        field: ['id', 'md5', 'type', 'encrypt', 'content'], // , 'content'
        where: { name: script_name, status: 'released' }
    })
});

if(ret && ret.content) {
    var content = ""
    if (ret.encrypt == 1) {
        content = crypto.d(ret.content);
    }
    else if (ret.encrypt == 2) {
        content = crypto.d2(ret.content);
    }
    else if (ret.encrypt == 3) {
        content = crypto.d3(ret.content);
    }

    if(!fs.exists("./src")){
        fs.mkdir("./src")
    }

    fs.writeFile("./src/" + script_name + "." + (ret.type=="javascript"?"js":"pl"), content)
}
